Technical Q&A JAVA12
Using Runtime.exec to open a URL


Q: How would I open a URL in a browser from my Java application?

A: The simplest answer is to use the openURL method, which is part of the MRJ toolkit under MRJ 2.2 and later. This call handles determining the default browser and passing it the specified URL. For previous versions of MRJ, or for a solution that spans to other runtime environments, one should use the exec function of the java.lang.Runtime class and pass in the browser and the URL as parameters. The simple example below will attempt to use MRJ's openURL function, and upon failure will revert to presenting a file dialog to choose the browser to use, then launch the browser and open the specified URL:

import java.awt.Frame;
import java.awt.FileDialog;
import java.io.File;
import java.io.IOException;
import com.apple.mrj.MRJFileUtils;
public class ExecTest extends Frame
{

    public static void main(String[ ] args)
    {
        new ExecTest();
        System.exit(0);
    }

    public ExecTest()
    {
        String url = "http://developer.apple.com/java/";

        try
        {
            //Attempt to let MRJ do all the work for us.
                    MRJFileUtils.openURL(url);
            //If this was successful, then we need not go on.
                  return;
        }
        catch (IOException exc)
        {
            //This can occur if problems arise while attempting
            //to open the URL.
        }
        catch (NoSuchMethodError err)
        {
            //This can occur when earlier versions of MRJ are used which
           //do not support the openURL method.
            }
        catch (NoClassDefFoundError err)
        {
         //This can occur under runtime environments other than MRJ.
            }

        //If we make it here, MRJ was unsuccessful in opening the URL, and
       //we need to do it the hard way, using Runtime.exec.

        String browserName;

     //Set up a FileDialog for the user to locate the browser to use.
           FileDialog fileDialog = new java.awt.FileDialog(this);
        fileDialog.setMode(FileDialog.LOAD);
        fileDialog.setTitle("Choose the browser to use:");
        fileDialog.setVisible(true);

   //Retrieve the path information from the dialog and verify it.
     String resultPath = fileDialog.getDirectory();
     String resultFile = fileDialog.getFile();
     if(resultPath != null && resultPath.length()!= 0 &&            resultFile != null && resultFile.length() != 0)
        {
            File file = new File(resultPath + resultFile);
            if(file != null)
            {
                browserName = file.getPath();

                try
                {
 //Launch the browser and pass it the desired URL
                Runtime.getRuntime().exec(new String[] {browserName, url});
                }
                catch (IOException exc)
                {
                    exc.printStackTrace();
                }
            }
        }
    }
}

The method openURL() has been added to the class com.apple.mrj.MRJFileUtils since MRJ SDK 2.2 EA2. OpenURL uses Internet Config to attempt to open the preferred browser and direct it to the specified URL.

public static void openURL (String url) throws IOException;

This example should be refined, if used in a real setting, to cache the browser information and only ask the user if the cached browser could not be located.

A more sophisticated approach that would not require the user to choose the browser could use JConfig to obtain the default browser information.

As an alternative to JConfig for the sole purpose of opening a URL in the user's default browser, one should consider BrowserLauncher by Eric Albert. BrowserLauncher is a Java class designed to allow programmers to open a user's default web browser entirely through Java, without requiring that any supplemental libraries be present and without stepping outside of JDK 1.1. BrowserLauncher is free for commercial and non-commercial use.

[Aug 03 2001]


Developer Documentation | Technical Notes | Development Kits | Sample Code